package version1

import (
	"server/global"
	entry "server/model/entrance"
	operatemodel "server/model/operate"
	"server/myutils"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

//@Summary 获取所有文章
//@Router /v1/getallarticle [get]
//@Tags Gets
//@Description 获取所有文章的简略信息
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func GetAllArticles(req *gin.Context) {
	data, code := operatemodel.Getallarticle()
	if code != global.SUCCESS {
		myutils.ResultJSON(req, code, nil)
		return
	}
	myutils.ResultJSON(req, code, data)
}

//@Summary 上传博客
//@Router /v1/uploadarticle [post]
//@Param data body entry.Uploadarticle true "categoryname必须是已经存在的分类名嗷"
//@Tags Posts
//@Description 需要权限等级为超级管理员才能执行此权限
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func UploadArticle(req *gin.Context) {
	var bind = new(entry.Uploadarticle)
	if req.ShouldBind(bind) != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	myutils.ResultJSON(req, operatemodel.Uploadarticle(bind), nil)
}

//@Summary 查看某个具体文章
//@Router /v1/getonearticle/:articleid [get]
//@Param id path int true "查找的文章的id"
//@Tags Gets
//@Description 无
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func GetOneArticle(req *gin.Context) {
	//用uri获取的数据都是string
	s := req.Param("id")
	//哈哈幸好我记得转换函数
	i, err := strconv.Atoi(s)
	if err != nil {
		myutils.ResultJSON(req, global.REDIRECT, nil)
		return
	}
	code, c := operatemodel.Getonearticle(i)
	if code != global.SUCCESS {
		myutils.ResultJSON(req, code, nil)
		return
	}
	myutils.ResultJSON(req, code, c)
}

//@Summary 删除某篇文章
//@Router /v1/deletearticle/:id [delete]
//@Param id path uint true "删除的文章id"
//@Tags Deletes
//@Description 需要权限等级为超级管理员才能执行此权限
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func DeleteArticle(req *gin.Context) {
	var id = req.Param("id")
	i, err := strconv.Atoi(id)
	if err != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	myutils.ResultJSON(req, operatemodel.Deletearticle(i), nil)
}

//@Summary 编辑更新文章
//@Router /v1/updataarticle [put]
//@Param data body entry.Updatarticle true "更新文章所需要的参数信息"
//@Tags Updatas
//@Description 需要权限等级为超级管理员才能执行此权限
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func UpdatArticle(req *gin.Context) {
	var bind = new(entry.Updatarticle)
	//json数据都是string识别不到unint所以要做转化
	if req.ShouldBindJSON(bind) != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, bind)
		return
	}
	myutils.ResultJSON(req, operatemodel.Updatarticle(bind), nil)
}

//@Summary 获取某个分类下的文章
//@Router /v1/getcategoryarticle/:cate  [get]
//@Param cate path  string true "传入文章分类,只能传入以下几个:`golang`,`web`,`other`,`chat`"
//@Tags Gets
//@Description  其他分类的文章,被放入other分类中
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func GetCategoryArticle(req *gin.Context) {
	s := req.Param("cate")
	if s != "go" && s != "web" && s != "other" && s != "chat" {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	data, code := operatemodel.Getcategoryarticle(s, false)
	myutils.ResultJSON(req, code, data)
}

//@Summary 获取某篇文章的评论
//@Router  /v1/getcommentofarticle/:id/:pagenum  [get]
//@Param id path string true "传入文章id,和分页数(从1开始),以换取分页评论"
//@Tags Gets
//@Description  普通权限即可获取
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func GetCommentOfArticle(req *gin.Context) {
	s := req.Param("id")
	i, err := strconv.Atoi(s)
	if err != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	y := req.Param("pagenum")
	i2, err2 := strconv.Atoi(y)
	if err2 != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	comment, code := operatemodel.Getcommentofarticle(i, i2)
	if code != global.SUCCESS {
		myutils.ResultJSON(req, code, nil)
		return
	}
	myutils.ResultJSON(req, code, comment)
}

//@Summary 上传用户评论
//@Router  /v1/postcomment/:id [post]
//@Param data body entry.Comment true "需要上传的信息"
//@Tags Posts
//@Description 上传
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func PostComment(req *gin.Context) {
	//先获取信息先
	var Aid = req.Param("id")
	i2, err := strconv.Atoi(Aid)
	if err != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	var bind = new(entry.Comment)
	if req.ShouldBindJSON(bind) != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, bind)
		return
	}
	i := operatemodel.Loginorsign(bind) //确保用户一定在册
	if i != global.SUCCESS {
		myutils.ResultJSON(req, i, nil)
		return
	}
	i3 := operatemodel.Addcomment(i2, bind.Username, bind.Email, bind.Context)
	myutils.ResultJSON(req, i3, nil)

}

//@Summary 获取某篇文章的评论数
//@Router /v1/getnumbofcomment/:id [get]
//@Param id path string true "传入需要获取的文章的id"
//@Tags Gets
//@Description
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func GetNumbOfComment(req *gin.Context) {
	s := req.Param("id")
	i, err := strconv.Atoi(s)
	if err != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	var num = operatemodel.Getnumofcomment(i)
	if num != -1 {
		myutils.ResultJSON(req, global.SUCCESS, num)
		return
	}
	myutils.ResultJSON(req, global.FAIL, nil)
}

//@Summary 博客查找
//@Router /v1/searchblog/:keyword [get]
//@Param keyword path string true "关键字"
//@Tags Gets
//@Description 用keyword模糊查询博客
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func SearchBlog(req *gin.Context) {
	s := req.Param("keyword")
	//看有没有空格,有空格进行划分
	sliceStr := strings.Split(s, " ")
	//然后去多余空格
	for k, v := range sliceStr {
		sliceStr[k] = strings.TrimSpace(v)
	}
	code, modata := operatemodel.Searchblog(sliceStr)
	myutils.ResultJSON(req, code, modata)
}

//@Summary 获取时间轴的数据
//@Router /v1/timestamp [get]
//@Tags Gets
//@Description 无特别说明
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func GetTimeStamp(req *gin.Context) {
	code, xin := operatemodel.Gettimestamp()
	myutils.ResultJSON(req, code, xin)
}
